package cn.chenshiwu121.service.impl;

import cn.chenshiwu121.mapper.SearchItemMapper;
import cn.chenshiwu121.pojo.SearchItem;
import cn.chenshiwu121.service.SearchService;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchItemMapper mapper;

    @Autowired
    private HttpSolrServer solrServer;

    @Override
    public void importAllSearchItems() throws IOException, SolrServerException {
        List<SearchItem> searchItemList = mapper.getSearchItemList();
        for (SearchItem item: searchItemList) {
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", item.getId().toString());
            document.addField("item_title", item.getTitle());
            document.addField("item_sell_point", item.getSell_point());
            document.addField("item_price", item.getPrice());
            document.addField("item_image", item.getImage());
            document.addField("item_desc", item.getItem_desc());
            document.addField("item_category_name", item.getCategory_name());
            solrServer.add(document);
        }
        solrServer.commit();
    }
}
